package com.youlu.campus.admin.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author: YangJ
 * @date: 2019/8/7 14:43
 */
public class MD5Util {

    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            String md5=new BigInteger(1, md.digest()).toString(16);
            return fillMD5(md5);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
        }
    }

    public static String fillMD5(String md5){
        return md5.length()==32?md5:fillMD5("0"+md5);
    }

    public static void main(String[] args) throws Exception{
//        long timeStampSec = System.currentTimeMillis()/1000;
//        String timestamp = String.format("%010d", timeStampSec);
//        String str = "goodsId=1718attrvalueId=557279,557282,557285,557287,557289,557298,557302,557306partner_id=1010timestamp="+timestamp+"C4DFC3B2671DD466";
//        System.out.println("md5:"+getMD5(str));
//        System.out.println("timeMillis:"+timestamp);

        long timeStampSec = System.currentTimeMillis();
        String timestamp = String.format("%010d", timeStampSec);
        System.out.println("timeMillis:"+timestamp);
        String str = "18610686432zg00475]%!m!0Go";
        System.out.println("md5:"+getMD5(str));
    }
}
